<?php
/**
 *
 * @authors Your Name (you@example.org)
 * @date    2018-01-10 17:14:34
 * @version $Id$
 */
namespace app\admin\controller;

use app\admin\model\AuthGroup as GroupModel;
use app\admin\model\AuthRule as RuleModel;
use Request;

class AuthGroup extends Base
{

    public function index($value = '')
    {
        $list = GroupModel::order('id', 'asc')->select();
        $this->assign([
            'list' => $list,
        ]);
        return $this->fetch();
    }

    /*
    信息添加
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\AuthGroup');
            if (true !== $result) {
                return $this->error($result);
            }
            if (isset($post['rules'])) {
                $post['rules'] = $post['rules'] ? implode(',', $post['rules']) : '';
            }
            $data = GroupModel::create($post);
            if (!empty($data)) {
                return $this->success('添加成功', 'admin/AuthGroup/index');
            }
        }
        $data = RuleModel::where('module', 'admin')->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /*
    信息编辑
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();
            //print_r($post);
            $result = $this->validate($post, 'app\admin\validate\AuthGroup.edit');
            if (true !== $result) {
                return $this->error($result);
            }
            if (isset($post['rules'])) {
                $post['rules'] = $post['rules'] ? implode(',', $post['rules']) : '';
            }
            $model = new GroupModel;
            $model = $model->allowField(true)->save($post, $id);

            if ($model) {
                return $this->success('修改成功', 'admin/AuthGroup/index');
            } else {
                return $this->error('修改失败');
            }
        }

        $info = GroupModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}

        $data = RuleModel::where('module', 'admin')->select();

        $rulesArr = explode(',', $info['rules']); //以前就拥有的权限节点
        foreach ($data as $k => $v) {
            if (in_array($v['id'], $rulesArr)) {
                $data[$k]['checked'] = 'y';
            } else {
                $data[$k]['checked'] = 'n';
            }
        }

        $this->assign([
            'data'   => $data,
            'info'   => $info,
            'typeid' => $id,
        ]);
        return $this->fetch();
    }

    /*
    信息删去
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = GroupModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/AuthGroup/index', '', 1);
            }
        }
        return $this->error('删去失败');
    }
    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
            if (input('?post.status')) {
                $data = ['id' => Request::post('id/d'), 'status' => Request::post('status')]; //状态
            } else if (input('?post.notation')) {
                $data = ['id' => Request::post('id/d'), 'notation' => Request::post('notation')]; //菜单
            }
            $model = GroupModel::update($data);
            if (!empty($model)) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        }
    }
}
